<?php
namespace Web\Controller;
use Think\Controller;
use Web\Logic\RoleLogic;
use Web\Logic\CommonLogic;
class SetParamController extends BaseController {
    
    public function cpn_list(){
        $param_name = $_REQUEST['param_name'];
        $var_type = $_REQUEST['var_type'];

        if(!empty($param_name)) $map['param_name'] = array('like', "%{$param_name}%");
        if(!empty($var_type)) $map['var_type'] = $var_type;

        $count = M('cpn_class_param')->where($map)->count();
        $Page  = $this->getpage($count,30);
        $Page->parameter['param_name']   =   urlencode($parm_name);
        $Page->parameter['var_type']   =   urlencode($var_type);

        $lists = M('cpn_class_param')
            ->alias('m')
            ->join('LEFT JOIN '.C('DB_PREFIX').'cpn_class cc ON cc.class_id=m.var_type')
            ->field("m.*,cc.class_name")
            ->limit($Page->firstRow.','.$Page->listRows)
            ->order('m.var_name asc')
            ->where($map)
            ->select();

        $class_list = M('cpn_class')->where("is_show= 1")->select();
        $this->assign("cpn_class",$class_list);

        $this->assign("list",$lists);
        $show  = $this->bootstrap_page_style($Page->show());

        $this->assign('empty','<td colspan="4">没有数据</td>');
        $this->assign('page',$show);
         $this->assign('param_name',$param_name);
         $this->assign('var_type',$var_type);
        $this->display();

    }



        /**
     * @desc 添加用户
     * @param rolename 用户名
     * @param notes 备注
     */
    public function cpn_add()
    {
        //实例化 logic
        //获取提交隐含域
        $subs = $_REQUEST['sub'];
        //接受用户名
        $data = $_REQUEST['data'];
        if($data)
        {

           $data['var_name'] = hexdec($data['var_name']);
           $data['is_show'] = 1;
           $r = M('cpn_class_param')->add($data);
           $this->success('增加成功');
        }else{
      

        $class_list = M('cpn_class')->where("is_show= 1")->select();
        $this->assign("cpn_class",$class_list);
        //调用模板
        $this->display();
        }
    }

    /**
     * @desc 用户删除
     * @param roleid 用户id
     */
    public function cpn_deletes()
    {
        //接受 roleid
        $id = $_REQUEST['id'];
        $r = M('cpn_class_param')->where(array('id'=>$id))->delete();

        if($r)
        {
            $this->success('删除成功');
        }
    }

      public function cpn_copys()
    {
        //接受 roleid
        $id = $_REQUEST['id'];
        $r = M('cpn_class_param')->where(array('id'=>$id))->find();
        $r['param_name'] = $r['param_name']."copy";
        unset($r['id']);

        if($r)
        {
            $r = M('cpn_class_param')->add($r);
            $this->success('复制成功');
        }
    }

    /**
     * @desc 用户修改
     * @param roleid 用户id
     */
    public function cpn_edit()
    {
        //接受 用户id
        $id = $_REQUEST['id'];

        //接受表单提交
        $sub = $_REQUEST['sub'];
        //接受修改信息
        $data = $_REQUEST['data'];
        if($data && $sub)
        {
           $data['var_name'] = hexdec($data['var_name']);
           $data['is_show'] = 1;
           $r = M('cpn_class_param')->where(array('id'=>$id))->save($data);
           $this->success('修改成功',"cpn_list");

        } else{


            //查询信息
            $datalist = M('cpn_class_param')->where(array('id'=>$id))->find();
            $class_list = M('cpn_class')->where("is_show= 1")->select();
            $this->assign("cpn_class",$class_list);
            $this->assign('datalist',$datalist);
            $this->display();
        }
    }
    
    
     public function device_list(){
        $param_name = $_REQUEST['param_name'];
        $var_type = $_REQUEST['var_type'];

        if(!empty($param_name)) $map['m.device_param_name'] = array('like', "%{$param_name}%");
        if(!empty($var_type)) $map['cg.id'] = $var_type;

        $count = M('device_class_param')->alias('m')
        ->join('LEFT JOIN '.C('DB_PREFIX').'cpn_class_group cg ON cg.cpn_type = m.cpn_type and cg.group_code = m.group_code ')
        ->where($map)->count();
        $Page  = $this->getpage($count,30);
        $Page->parameter['param_name']   =   urlencode($parm_name);
        $Page->parameter['var_type']   =   urlencode($var_type);

        $lists = M('device_class_param')
            ->alias('m')
            ->join('LEFT JOIN '.C('DB_PREFIX').'device_sensor cc ON cc.sensor_code=m.sensor_type')
            ->join('LEFT JOIN '.C('DB_PREFIX').'device_control dc ON dc.control_code=m.control_type')
            ->join('LEFT JOIN '.C('DB_PREFIX').'cpn_class_group cg ON cg.cpn_type = m.cpn_type and cg.group_code = m.group_code ')
            ->field("m.*,cc.sensor_name,cg.group_name,dc.control_name")
            ->limit($Page->firstRow.','.$Page->listRows)
            ->order('m.id asc')
            ->where($map)
            ->select();

        $class_list = M('cpn_class_group')->where("is_show= 1")->order("cpn_type,group_code")->select();
        $this->assign("cpn_class",$class_list);

        $this->assign("list",$lists);
        $show  = $this->bootstrap_page_style($Page->show());

        $this->assign('empty','<td colspan="4">没有数据</td>');
        $this->assign('page',$show);
         $this->assign('param_name',$param_name);
         $this->assign('var_type',$var_type);
        $this->display();

    }



        /**
     * @desc 添加用户
     * @param rolename 用户名
     * @param notes 备注
     */
    public function device_add()
    {
        //实例化 logic
        //获取提交隐含域
        $subs = $_REQUEST['sub'];
        //接受用户名
        $data = $_REQUEST['data'];
        if($data)
        {
           $data['cpn_param_id'] = implode(",",$data['cpn_param_id']);
           $data['is_show'] = 1;
           $r = M('device_class_param')->add($data);
           $this->success('增加成功');
        }else{
      

        $class_list = M('cpn_class')->where("is_show= 1")->select();
        $this->assign("cpn_class",$class_list);
        
        $sensor_list = M('device_sensor')->select();
        $this->assign("sensor_list",$sensor_list);
        
        $control_list = M('device_control')->select();
        $this->assign("control_list",$control_list);
        //调用模板
        $this->display();
        }
    }

    /**
     * @desc 用户删除
     * @param roleid 用户id
     */
    public function device_deletes()
    {
        //接受 roleid
        $id = $_REQUEST['id'];
        $r = M('device_class_param')->where(array('id'=>$id))->delete();

        if($r)
        {
            $this->success('删除成功');
        }
    }

      public function device_copys()
    {
        //接受 roleid
        $id = $_REQUEST['id'];
        $r = M('device_class_param')->where(array('id'=>$id))->find();
        $r['device_param_name'] = $r['device_param_name']."copy";
        unset($r['id']);

        if($r)
        {
            $r = M('device_class_param')->add($r);
            $this->success('复制成功');
        }
    }

    /**
     * @desc 用户修改
     * @param roleid 用户id
     */
    public function device_edit()
    {
        //接受 用户id
        $id = $_REQUEST['id'];

        //接受表单提交
        $sub = $_REQUEST['sub'];
        //接受修改信息
        $data = $_REQUEST['data'];
        if($data && $sub)
        {
           $data['is_show'] = 1;
           $data['cpn_param_id'] = implode(",",$data['cpn_param_id']);
           $r = M('device_class_param')->where(array('id'=>$id))->save($data);
           $this->success('修改成功',"device_list");
        } else{

            //查询信息
            $datalist = M('device_class_param')->where(array('id'=>$id))->find();
            $this->assign('datalist',$datalist);
            
            $class_list = M('cpn_class')->where("is_show= 1")->select();
            $this->assign("cpn_class",$class_list);
            
            $sensor_list = M('device_sensor')->select();
            $this->assign("sensor_list",$sensor_list);
            
            $control_list = M('device_control')->select();
            $this->assign("control_list",$control_list);
        
            
            $this->display();
        }
    }
    
     public function getGroupCode(){
        $cpn_type = I('cpn_type', 0);
        $Cpn = M('cpn_class_group');
        $group_list = $Cpn->where(array('cpn_type'=>$cpn_type))->getField('group_code,group_name',true);

        $this->ajaxReturn($group_list);

    }
    
    public function getCpnParam(){
        $cpn_type = I('cpn_type', 0);
        $Cpn = M('cpn_class_param');
        $cpn_list = $Cpn->where(array('cpn_type'=>$cpn_type))->field('id,param_name,var_name')->select();
        $showList = array();
        foreach($cpn_list as $k =>$v){
            $showList[$v['id']] = $v['param_name']."(".dechex($v['var_name']).")";   
        }
        $this->ajaxReturn($showList);

    }
    
    




    
}